package com.ruyuan.commodity.common.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.ruyuan.commodity.model.goodssearch.GoodsWords;
import com.ruyuan.commodity.model.goodssearch.GoodsWordsDO;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.ResultType;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;
import java.util.List;

/**
 * GoodsWords的Mapper
 */
@Repository
public interface GoodsWordsMapper extends BaseMapper<GoodsWordsDO> {

    /**
     * 查询商品分词的和对应数量
     * @param keyword 关键词
     * @return 分词和数量对应集合
     */
    @ResultType(GoodsWords.class)
    @Select("select words,goods_num from es_goods_words " +
            "where words like #{keyword} or quanpin like #{keyword} " +
            "or szm like #{keyword} order by sort desc limit 15")
    List<GoodsWords> getGoodsWords(@Param("keyword")String keyword);

    /**
     * 增加分词的数量
     * @param words 分词
     */
    @Update("update es_goods_words set goods_num=goods_num+1 where words=#{words}")
    void addGoodsNum(@Param("words") String words);

    /**
     * 查询分词集合
     * @param type GoodsWordsType
     * @return
     */
    @ResultType(String.class)
    @Select("select words from es_goods_words where `type` = #{type}")
    List<String> selectWordsList(@Param("type") String type);

    /**
     * 减少分词的数量
     * @param words 分词
     */
    @Update("update es_goods_words set goods_num  =  goods_num-1  where  goods_num-1 >=0 and  words=#{words}")
    void reduceGoodsNum(String words);
}
